<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>查看日记</title>
    <link rel="icon" href="pic/favicon.ico" type="image/x-icon">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        /* 全局样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            min-height: 100vh;
            background-image: url('./pic/绿色.png'); /* 替换为你的背景图URL */
            background-size: cover; /* 背景图覆盖整个页面 */
            background-position: center; /* 背景图居中 */
            background-attachment: fixed; /* 背景图固定 */
            display: flex;
            justify-content: center;
            align-items: center;
        }

        /* 容器 */
        .container {
            display: flex;
            background-color: rgba(255, 255, 255, 0.9); /* 半透明背景 */
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            opacity: 0.8;
        }

        /* 左侧盒子 */
        .sidebar {
            width: 400px; /* 固定宽度 */
            height: 600px; /* 固定高度 */
            background-color: rgba(244, 244, 244, 0.9); /* 半透明背景 */
            padding: 20px;
            box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        .sidebar h2 {
            margin-top: 0;
            height: 40px; /* 固定高度 */
            display: flex;
            align-items: center; /* 垂直居中 */
        }

        .label-list {
            flex: 1; /* 填充剩余空间 */
            overflow-y: auto; /* 如果内容过多，允许滚动 */
            display: flex;
            flex-direction: column;
            gap: 10px; /* 设置子元素之间的间距 */
        }

        .label-item {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .label-item:hover {
            background-color: rgba(0, 123, 255, 0.1); /* 鼠标悬停时浅蓝色背景 */
        }

        /* 右侧盒子 */
        .content {
            width: 600px; /* 固定宽度 */
            height: 600px; /* 固定高度 */
            padding: 20px;
            background-color: rgba(255, 255, 255, 0.9); /* 半透明背景 */
            display: flex;
            flex-direction: column;
        }

        .content h2 {
            margin-top: 0;
            height: 40px; /* 固定高度 */
            display: flex;
            align-items: center; /* 垂直居中 */
        }

        .diary-list {
            flex: 1; /* 填充剩余空间 */
            overflow-y: auto; /* 如果内容过多，允许滚动 */
            display: flex;
            flex-direction: column;
            gap: 10px; /* 设置子元素之间的间距 */
        }

        .diary-item {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .diary-item:hover {
            background-color: rgba(0, 123, 255, 0.1); /* 鼠标悬停时浅蓝色背景 */
        }

        /* 删除按钮样式 */
        .delete-button {
            background-color: #ff4d4d; /* 红色背景 */
            color: white; /* 白色文字 */
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .delete-button:hover {
            background-color: #ff1a1a; /* 鼠标悬停时更深的红色 */
        }
    </style>
</head>
<body>
<div id="app">
    <!-- 容器 -->
    <div class="container">
        <!-- 左侧盒子 -->
        <div class="sidebar">
            <h2>日记标签</h2>
            <div class="label-list">
                <div v-for="label in labels" :key="label" class="label-item" v-on:click="loadDiaries(label)">
                    {{ label }}
                </div>
            </div>
        </div>

        <!-- 右侧盒子 -->
        <div class="content">
            <h2></h2>
            <div class="diary-list">
                <div v-for="diary in diaries" :key="diary.id" class="diary-item">
                    <div>
                        <h3 v-on:click="viewDiaryDetail(diary.id)">{{ diary.title }}</h3>
                        <p>{{ diary.date_time }}</p>
                    </div>
                    <button class="delete-button" v-on:click="deleteDiary(diary.id)">删除</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // Vue 对象
    const app = {
        // 数据
        data() {
            return {
                labels: [], // 所有标签
                selectedLabel: '', // 当前选中的标签
                diaries: [] // 当前标签下的日记列表
            };
        },
        // 方法
        methods: {
            // 加载所有标签
            loadLabels() {
                const user = JSON.parse(localStorage.getItem('user'));
                if (!user) {
                    alert('用户未登录，请先登录');
                    window.location.href = '/login.html';
                    return;
                }
                axios({
                    method: 'get',
                    url: '/diary/labels',
                    params: { user_id: user.id }
                }).then((response) => {
                    this.labels = response.data;
                }).catch((error) => {
                    console.error('获取标签失败:', error);
                });
            },
            // 根据标签加载日记列表
            loadDiaries(label) {
                this.selectedLabel = label;
                const user = JSON.parse(localStorage.getItem('user'));
                if (!user) {
                    alert('用户未登录，请先登录');
                    window.location.href = '/login.html';
                    return;
                }
                axios({
                    method: 'get',
                    url: '/diary/list',
                    params: { user_id: user.id, label }
                }).then((response) => {
                    this.diaries = response.data;
                    console.log(this.diaries);
                }).catch((error) => {
                    console.error('获取日记列表失败:', error);
                });
            },
            //删除日记
            deleteDiary:function (id){
                axios({
                    method: 'get',
                    url: '/deleteDiary',
                    params: {id:id }
                }).then((response) => {
                    alert(response.data)
                }).catch((error) => {
                    console.error('删除失败:', error);
                });
            },
            // 查看日记详情
            viewDiaryDetail(id) {
                window.location.href = `/diary-detail.html?id=${id}`;
            }
        },
        // 构造
        mounted() {
            console.log("Vue启动成功")
            this.loadLabels();

        }
    };

    Vue.createApp(app).mount("#app");
</script>
</body>
</html>